package net.openl10n.packaging.document.comments;

import net.openl10n.api.document.comments.SimpleComments;
import net.openl10n.opc4j.PackagePart;
import net.openl10n.packaging.AbstractJaxbContentPart;
import net.openl10n.packaging.OpenLocPartContainer;
import net.openl10n.packaging.OpenLocPartFactory;
import net.openl10n.packaging.OpenLocPartFactoryManager;

public class SimpleCommentsPart extends AbstractJaxbContentPart{
	
	public static final String CONTENT_TYPE = "extension/openl10n.general.comments+xml";
	public static final String TARGET_NAME = "/comments.xml";
	public static final String RELATIONSHIP_TYPE = "http://schemas.openl10n.net/document/comments";

	private SimpleComments simpeComments;
	
	public static final OpenLocPartFactory<SimpleCommentsPart> FACTORY = 
		new OpenLocPartFactory<SimpleCommentsPart>() {
			@Override
			public SimpleCommentsPart create(OpenLocPartContainer container, PackagePart part, OpenLocPartFactoryManager partFactoryManager) {
				return new SimpleCommentsPart(container, part, partFactoryManager);
			}

			@Override
			public String getContentType() {
				return CONTENT_TYPE;
			}

			@Override
			public String getRelationshipType() {
				return RELATIONSHIP_TYPE;
			}

			@Override
			public Class<SimpleCommentsPart> getTargetClass(){
				return SimpleCommentsPart.class;
			}
			
			@Override
			protected String getPartNamePattern() {
				return TARGET_NAME;
			};
		};
	
	public SimpleCommentsPart(OpenLocPartContainer container, PackagePart part, OpenLocPartFactoryManager partFactoryManager) {
		super(container, part, partFactoryManager); 
	}

	public SimpleComments getSimpleComments() {
		if(simpeComments == null)
			simpeComments = loadOrCreateJaxbObject(SimpleComments.class);
		return simpeComments;
	}
	
	public void setSimpleComments(SimpleComments simpleComments) {
		this.simpeComments = simpleComments;
	}
	
	@Override
	protected void onClose() {
		if(simpeComments != null)
			saveJaxbObject(simpeComments);
		super.onClose();
	}
	
}
